
$(function(){
	
});
var sections = [];

function createSectionElem(section){
	var sElem = "<li><span class=\"charpterName\">"+section.no+"."+section.title+"</span><span class=\"charpterTime\">"+section.time+"</span></li>";
	return sElem;
}

function readySection(section){
	addSection(section);
	sections.push(section);
}

function addSection(section){
	var sElem = createSectionElem(section);
	var $elem = $(sElem);
	$("#charpterContainer ol").append($elem);
	$elem.bind("click", sectionSeekVideo);
}

function sectionSeekVideo(event){
	focusLi($(this));
	var idx = $(this).index("li");
	var section = sections[idx];
	info("[section][sectionSeekVideo]modify video's currentTime to:", section.begin);
	$("#video")[0].currentTime = section.begin;
}

function getSection(time){
	var cur = null;
	for(var i in sections){
		var section = sections[i];
		if(section.begin <= time){
			cur = section;
		}else{
			break;
		}
	}
	info("[section.js][getSection] time:"+time+", section:",cur);
	return cur;
}

function getSectionByIdx(idx){
	return sections[idx];
}

function focusSection(section){
	if(section!=null){
		var li = $("#charpterContainer li").get(section.no - 1);
		if(li){
			focusLi($(li));
		}else{
			warn("[focusSection] section:", section);
		}
	}
}

function focusLi($li){
	$("#charpterContainer").find(".current").removeClass("current");
	$li.addClass("current");
	$("#charpterContainer").scrollTop($li.index("li")*17);
	debug("[section][focusLi] scroll top:", $li.index("li")*17);
}